package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    /**
     * 预约业务
     */
    @PostMapping("/submit")
    public Result submit(@RequestBody Map map) {
        //取出参数
        String setmealId = (String) map.get("setmealId");
        String idCard = (String) map.get("idCard");
        String sex = (String) map.get("sex");
        String name = (String) map.get("name");
        String validateCode = (String) map.get("validateCode");
        String telephone = (String) map.get("telephone");
        String orderDate = (String) map.get("orderDate");


        //检查验证码是否正确
        String redisCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        if (redisCode == null || !redisCode.equals(validateCode)) {
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        //新建空的返回集
        //进行预约业务
        Result result = null;
        try {
            //注入预约类型
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            result = orderService.add(map);
            //预约成功发送通知短信
            //判空防止空指针
            if (result != null) {
                if (result.isFlag()) {
                    //先注释
                    if (false) {
                        SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, telephone, orderDate);
                    }
                    System.out.println("*********您的电话为：" + telephone + "日期为：" + orderDate + "***********");

                    return result;
                }
            }
        } catch (ClientException e) {
            e.printStackTrace();
            //发送短信失败 记录日志 人工处理发送短信 错误机率比较低
            //return new Result(false, MessageConstant.ORDER_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            //预约业务失败
            return new Result(false, MessageConstant.ORDER_FAIL);
        }
        return result;
    }

    /**
     *
     * @param id order表的id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Map map = orderService.findById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
